<template>
	<view class="addr-view">
		<view class="addr-list" v-if="addrList && isLoading">
			<view class="addr-item" @tap="selectAddress(item)" v-for="(item, index) in addrList" :key="item.id">
				<AddrItem :isStore="isStore" @selectShopAddr="selectShopAddr" @setDef="setDef" @deleteItem="deleteItem" :item="item" :index="index"></AddrItem>
			</view>
		</view>
		
		<view class="not-order" v-if="!addrList && !isLoading">
			<image src="../../static/icon/image_34.png" class="not-img" mode=""></image>
			<view class="not-desc">{{$t("addrManage.hint")}}</view>
		</view>

		<view class="receive-btn" v-if="!isStore" @click="goEditAddr">
			<text class="plus">{{$t("addrManage.plus")}}</text>{{$t("addrManage.addAddr")}}
		</view>
		
		<u-modal width="500rpx" :show="show" :content='content' :cancelText="$t('common.cancel')" confirmColor="#000" :confirmText="$t('common.confirm')" @close="show=false" @cancel="show=false" @confirm="confirm" closeOnClickOverlay showCancelButton></u-modal>
	</view>
</template>

<script>
	import AddrItem from "../../components/AddrItem/AddrItem"
	import { getUserAddr, deleteUserAddr, setAddrdef, getShop, updateOrder } from "@/common/api.js"

	export default {
		components: {
			AddrItem
		},
		data() {
			return {
				addrList: null,
				show: false,
				content: this.$t("editAddr.hint4"),
				id: null,
				index: null,
				isOrder: null,
				isLoading: true,
				isPya: null,
				isStore: null,
				isLook: null,
				isGoPay: null
			}
		},
		methods: {
			async selectAddress(item) {
		
				if(this.isLook) {
				
					return
				}
				
				if(this.isStore) {
					uni.setStorageSync("companyAddr", item);
					uni.navigateBack();
				} else if(this.isGoPay) {
				
					// return
					const res = await updateOrder({
						id: this.isGoPay,
						addressId: item.id,
						orderState: this.$route.query.orderState,
						orderStatus: this.$route.query.orderStatus,
					});
			
					if(res.code == 200) {
						uni.navigateBack()
					} else {
						uni.showToast({
							icon: 'none',
							title: this.$t('editAddr.hint5'),
							duration: 1300,
						})
					}
					
				} else {
					uni.setStorageSync("selectAddr", item);
					uni.navigateBack();
				}
				// uni.redirectTo({
				// 	url: `/pages/confirmGoodsOrder/confirmGoodsOrder?addrItem=${encodeURIComponent(JSON.stringify(item))}`
				// })
				
				
				// if(this.isPya) {
				// 	console.log("点击", item);
				// 	uni.setStorageSync("selectAddr", item);
				// 	// uni.redirectTo({
				// 	// 	url: `/pages/confirmGoodsOrder/confirmGoodsOrder?addrItem=${encodeURIComponent(JSON.stringify(item))}`
				// 	// })
				// 	uni.navigateBack();
				// }
			},
			async setDef(value) {
				this.addrList.forEach((item, index) => {
					if(value.id === item.id) {
						return
					}
					this.addrList[index].ifDefault = "0";
				});
				
				value.ifDefault == "0" ? value.ifDefault = "1":value.ifDefault = "0";
				const res = await setAddrdef(value);
				if(res.code == 200) {
					uni.setStorageSync("defAddr", value)
				}
			},
			selectShopAddr() {
				
			},
			async confirm() {
				this.show = false;
				this.addrList.splice(this.index, 1);
				const res = await deleteUserAddr(this.id);
				
			},
			// 父组件处理删除事件
			deleteItem(id, index) {
				// console.log("删除地址id===========", id, index);
				this.show = true;
				this.id = id;
				this.index = index;
			},
			goEditAddr() {
				uni.navigateTo({
					url: "/pages/editAddr/editAddr"
				})
			},
			async getAddrList() {
				if(this.isStore) {
					
					const {rows} = await getShop();
				
					if (!rows || rows.length <= 0) {
						this.isLoading = false;
						return
					} else {
						
						this.isLoading = true;
						this.addrList = rows;
					}
				} else {
					const {rows} = await getUserAddr({
						pageSize: 1000,
						userId: uni.getStorageSync("userId")
					});
					
					
					let temp = null;
					if (!rows || rows.length <= 0) {
						this.isLoading = false;
						return
					} else {
						
						this.isLoading = true;
						this.addrList = rows;
					}
					for (let i = 0; i < rows.length; i++) {
						if (rows[i].ifDefault == "1") {
							temp = rows[i];
							break;
						}
					}
					
					if (temp) {
						uni.setStorageSync("defAddr", temp)
					}
				}
			}
		}, 
		onLoad(option) {
			console.log(option);
			this.isOrder = option.order;
			this.isPay = option.pay;
			if(option.store) {
				this.isStore = option.store;
			}
			if(option.look) {
				this.isLook = option.look;
			}
			if(option.isGoPay) {
				this.isGoPay = option.isGoPay;
			}
		},
		onShow() {
			this.getAddrList();
			// console.log(this.$route.query ,"this.$route.query ")
			
		},
		onReady() {
			uni.setNavigationBarTitle({ title: this.$t("addrManage.title")});
		},
		// onBackPress(e) {
		// 	console.log(123);
		// 	uni.switchTab({
		// 		url: "/pages/my/my"
		// 	})
		// 	return true;
		// },
	}
</script>


<style lang="scss">
	page {
	  background: #FBFBFB;
	}
</style>
<style lang="scss" scoped>
	.not-order {
		position: absolute;
		left: 50%;
		top: 300rpx;
		transform: translateX(-50%);
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	
		.not-img {
			width: 136rpx;
			height: 154rpx;
		}
	
		.not-desc {
			color: #858585;
			font-family: PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			text-align: center;
			margin-top: 104rpx;
		}
	}
	
	.addr-view {
		// background-color: red;
		padding: 20rpx 24rpx;
		box-sizing: border-box;
		// margin-bottom: 88rpx;
		// position: absolute;
		// width: 100%;
		// height: 100%;
	}
	
	.addr-list {
		// padding-bottom: 108rpx;
	}

	.addr-item {
		border-radius: 20rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 2rpx 16rpx 0rpx #00000019;
		margin-top: 20rpx;
	}

	.addr-item:first-child {
		margin: 0;
	}

	.addr-item:last-child {
		margin-bottom: 100rpx;
	}
	
	.foot {
		margin-bottom: 100rpx;
	}

	.receive-btn {
		border-radius: 20rpx 20rpx 0 0;
		background: #F1F1F1;
		box-shadow: 0rpx 2rpx 16rpx 0rpx #00000019;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		color: #858585;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 36rpx;
		
		.plus {
			font-size: 45rpx;
		}
	}
</style>
